﻿using Microsoft.SharePoint;

namespace CSP.Core 
{
    public static class GroupsHelper
    { 
        public enum AssociatedGroupTypeEnum 
        {
            Owners, 
            Members, 
            Vistors
        }; 
                
        public static void SetAssociatedGroups(SPWeb spWeb, SPGroup[] groups) 
        {
            string formatString = "";
            object[] ids = new object[groups.Length];
            for (int i = 0; i < groups.Length; i++)
            { 
                formatString += string.Format("{{{0}}};", i); 
                ids[i] = groups[i].ID; 
            } 
            spWeb.Properties["vti_associategroups"] = string.Format(formatString.TrimEnd(new char[] { ';' }), ids);
            spWeb.Properties.Update(); 
        } 
        
        public static SPGroup AddGroup(SPWeb spWeb, string group, AssociatedGroupTypeEnum associateGroupType, bool copyUsersFromParent) 
        {
            ResourceFile resFile = new ResourceFile(spWeb.Locale.Name);

            switch (associateGroupType) 
            {
                case AssociatedGroupTypeEnum.Owners:
                    return AddGroup(spWeb, group, resFile.LoadResource("CSP_fullcontrol_description"), resFile.LoadResource("CSP_fullcontrol_title"), "vti_associateownergroup", spWeb.AssociatedOwnerGroup, copyUsersFromParent);
                case AssociatedGroupTypeEnum.Members:
                    return AddGroup(spWeb, group, resFile.LoadResource("CSP_contribute_description"), resFile.LoadResource("CSP_contribute_title"), "vti_associatemembergroup", spWeb.AssociatedMemberGroup, copyUsersFromParent); 
                case AssociatedGroupTypeEnum.Vistors:
                    return AddGroup(spWeb, group, resFile.LoadResource("CSP_read_description"), resFile.LoadResource("CSP_read_title"), "vti_associatevisitorgroup", spWeb.AssociatedVisitorGroup, copyUsersFromParent); 
                default:   
                    return null; 
            }
        }

        public static SPGroup AddGroup(SPWeb spWeb, string groupText, string descriptionFormatString, string roleDefinitionName, string associatedGroupName, SPGroup parentAssociatedGroup, bool copyUsersFromParent)
        { 
            SPGroup owner = parentAssociatedGroup; 
            if (associatedGroupName != "vti_associateownergroup")
                owner = spWeb.SiteGroups.GetByID(int.Parse(spWeb.Properties["vti_associateownergroup"]));
            spWeb.SiteGroups.Add(groupText.ToString(), owner, null, string.Format(descriptionFormatString, spWeb.Name));
            SPGroup group = spWeb.SiteGroups[groupText.ToString()];
            if (descriptionFormatString.IndexOf("{0}") != -1)
            { 
                SPListItem item = spWeb.SiteUserInfoList.GetItemById(group.ID); 
                item["Notes"] = string.Format(descriptionFormatString, string.Format("<a href=\"{0}\">{1}</a>", spWeb.Url, spWeb.Name)); 
                item.Update(); 
            } 
            if (roleDefinitionName != null)
            { 
                SPRoleAssignment roleAssignment = new SPRoleAssignment(group); 
                SPRoleDefinition roleDefinition = spWeb.RoleDefinitions[roleDefinitionName];
                roleAssignment.RoleDefinitionBindings.Add(roleDefinition); 
                spWeb.RoleAssignments.Add(roleAssignment); 
            }
            if (copyUsersFromParent && parentAssociatedGroup != null) 
                foreach (SPUser user in parentAssociatedGroup.Users)
                    group.AddUser(user);
            if (associatedGroupName != null) 
            { 
                spWeb.Properties[associatedGroupName] = group.ID.ToString();
                spWeb.Properties.Update(); 
            }
            spWeb.Update(); 
            return group; 
        } 
    } 
}